package com.fuqi.android.zhongcai.business.shuig.view.fragment;

import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.animation.ZoomEnter.ZoomInEnter;
import com.flyco.animation.ZoomExit.ZoomOutExit;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.flyco.dialog.widget.NormalDialog;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.application.UniApplication;
import com.fuqi.android.zhongcai.base.Constants;
import com.fuqi.android.zhongcai.base.view.activity.BaseActivity;
import com.fuqi.android.zhongcai.base.view.fragment.BaseFragment;
import com.fuqi.android.zhongcai.base.view.widget.CustomProgress;
import com.fuqi.android.zhongcai.base.view.widget.NormalViewDialog;
import com.fuqi.android.zhongcai.base.view.widget.TitleBar;
import com.fuqi.android.zhongcai.business.dinuan.view.activity.CompleteShowDiNuanActivity;
import com.fuqi.android.zhongcai.business.dinuan.view.activity.DiNuanCompleteActivity;
import com.fuqi.android.zhongcai.business.dinuan.view.activity.DiNuanMarkOrderActivity;
import com.fuqi.android.zhongcai.business.dinuan.view.activity.DiNuanPaiGongActivity;
import com.fuqi.android.zhongcai.business.shuig.OrderDict;
import com.fuqi.android.zhongcai.business.shuig.ShuiGBuz;
import com.fuqi.android.zhongcai.business.shuig.mvp.ShuiGContract;
import com.fuqi.android.zhongcai.business.shuig.view.activity.ChouChaShowActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.CommentShowActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.CompleteShowActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.HuiFangShowActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.ShuiGCannotInstallActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.ShuiGChouChaActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.ShuiGCommentActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.ShuiGCompleteActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.ShuiGHuiFangActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.ShuiGMakeOrderActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.ShuiGPaiDanActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.ShuiGPaiGongActivity;
import com.fuqi.android.zhongcai.business.shuig.view.widget.BGASortableNinePhotoLayoutView;
import com.fuqi.android.zhongcai.business.shuig.view.widget.CJRow.ARResponse;
import com.fuqi.android.zhongcai.business.shuig.view.widget.CJRow.CJRow;
import com.fuqi.android.zhongcai.business.shuig.view.widget.CJRow.CJRowListener;
import com.fuqi.android.zhongcai.business.shuig.view.widget.CJRow.CustomCJRowAdapter;
import com.fuqi.android.zhongcai.business.shuig.view.widget.CJRow.ViewBinder;
import com.fuqi.android.zhongcai.sql.Dic;
import com.fuqi.android.zhongcai.sql.User;
import com.fuqi.android.zhongcai.sql.dao.SQLDao;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.android.zhongcai.utils.dic.IField;
import com.fuqi.android.zhongcai.utils.dic.impl.Field;
import com.fuqi.android.zhongcai.utils.dic.impl.Table;
import com.fuqi.framework.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import cn.bingoogolapple.photopicker.activity.BGAPhotoPreviewActivity;

import static com.google.gson.internal.$Gson$Preconditions.checkNotNull;

/**
 * Created by Bruce on 2016/10/20.
 */
public class ShuiGFragment extends BaseFragment implements ShuiGContract.View ,BGASortableNinePhotoLayoutView.Delegate{
    @Bind(R.id.cjrow_base)
    CJRow mRowBase;
    @Bind(R.id.cjrow_client)
    CJRow mRowClient;
    @Bind(R.id.cjrow_status)
    CJRow mRowStatus;

    @Bind(R.id.bottom_lyt)
    LinearLayout BottomLyt;
    @Bind(R.id.btn_left)
    Button mBtnLeft;
    @Bind(R.id.btn_right)
    Button mBtnRight;

    CustomCJRowAdapter baseAdapter,clientAdapter,statusAdapter;
    Table table_base, table_client, table_status;
    int[] mTo = new int[] { R.id.plan_item, R.id.plan_item_detail };
    int layout = R.layout.cj_plan;
    ARResponse response;
    User user;
    String ordType;

    private ShuiGContract.Presenter mPresenter;
    static ShuiGFragment fragment;

    public static ShuiGFragment newInstance() {
        ShuiGFragment fragment = new ShuiGFragment();
        return fragment;
    }

    @Override
    public void setPresenter(@NonNull ShuiGContract.Presenter presenter) {
        mPresenter = checkNotNull(presenter);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @Override
    public void show(ARResponse response) {
        this.response = response;
        if (response != null) {
            String role = UniApplication.getInstance().getCurrentRole();
            ordType = response.getValue("ORDER_TYPE");
            if(Constants.TYPE_SHUI_GUAN.equals(ordType)){
                table_client = ShuiGBuz.getClientTable();
                table_status = ShuiGBuz.getStatusTable();
            }else{
                table_client = ShuiGBuz.getClientDNTable();
                table_status = ShuiGBuz.getStatusDNTable();
                if(OrderDict.RoleType.TYPE_GRKH.equals(role))
                    ShuiGBuz.getUserStatusTable(table_status);
            }

//            ShuiGBuz.makeHide(table_base,response);
//            ShuiGBuz.makeHide(table_client,response);
            ShuiGBuz.makeHide(table_status,response);
            clientAdapter.setTable(table_client);
            statusAdapter.setTable(table_status);
            baseAdapter.refresh(response);
            clientAdapter.refresh(response);
            statusAdapter.refresh(response);
        }
    }

    @Override
    public void call(String tel) {
        if(!tel.equals(user.getPhone()))
            ActivityUtils.callTo(getActivity(),tel);
    }

    @Override
    public void showButton(ARResponse r) {
        String status = r.getValue("ORDER_STATUS");
        String chargehandId = r.getValue("CHARGEHAND_ID");
        String role = UniApplication.getInstance().getCurrentRole();
        String visitId = r.getValue("VISIT_BY");
        String checkId = r.getValue("CHECK_BY");
//        String commentId = r.getValue("COMMENT_ID");
        String commentId = r.getValue("COMMENT_DATE");
        BottomLyt.setVisibility(View.GONE);
        mBtnLeft.setVisibility(View.GONE);
        mBtnRight.setVisibility(View.GONE);
        switch (status){
            case OrderDict.StatusType.DDZT_DSZ:
                if (OrderDict.RoleType.TYPE_NQ.equals(role)){
                    //  待审查，驳回/通过并派单
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnLeft.setVisibility(View.VISIBLE);
                    mBtnLeft.setText("驳回");
                    mBtnLeft.setTag(OrderDict.ActionType.ACTION_BH);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("通过并派单");
                    mBtnRight.setTag(OrderDict.ActionType.ACTION_TGPD);
                    mBtnRight.setEnabled(true);
                }else if(OrderDict.RoleType.TYPE_GRKH.equals(role)){
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("审核中");
                    mBtnRight.setEnabled(false);
                }
                break;
            case OrderDict.StatusType.DDZT_BTG:
                if (OrderDict.RoleType.TYPE_NQ.equals(role)||OrderDict.RoleType.TYPE_GRKH.equals(role)){
                    //  不通过，重新下单
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("重新下单");
                    mBtnRight.setTag(OrderDict.ActionType.ACTION_CXXD);
                    mBtnRight.setEnabled(true);
                }
                break;
            case OrderDict.StatusType.DDZT_DCL:
                if (OrderDict.RoleType.TYPE_SGDZ.equals(role)){
                    //  待处理，无法联系/确定接单 -->确定接单选择工人
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnLeft.setVisibility(View.VISIBLE);
                    mBtnLeft.setText("无法联系");
                    mBtnLeft.setTag(OrderDict.ActionType.ACTION_WFLX);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("确定派单");
                    mBtnRight.setTag(OrderDict.ActionType.ACTION_QRJD);
                    mBtnRight.setEnabled(true);
                }
                break;
            case OrderDict.StatusType.DDZT_DCF:
                if (OrderDict.RoleType.TYPE_SGGR.equals(role) && user.getUserid().equals(chargehandId)){
                    //  待出发，立即出发
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("立即出发");
                    mBtnRight.setTag(OrderDict.ActionType.ACTION_LJCF);
                    mBtnRight.setEnabled(true);
                }
                break;
            case OrderDict.StatusType.DDZT_DAZ:
                if (OrderDict.RoleType.TYPE_SGGR.equals(role) && user.getUserid().equals(chargehandId)){
                     //  待安装 不可安装/开始安装 --> 不可安装上传图片
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnLeft.setVisibility(View.VISIBLE);
                    mBtnLeft.setText("不可安装");
                    mBtnLeft.setTag(OrderDict.ActionType.ACTION_BKAZ);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("开始安装");
                    mBtnRight.setTag(OrderDict.ActionType.ACTION_KSAZ);
                    mBtnRight.setEnabled(true);
                }
                break;
            case OrderDict.StatusType.DDZT_AZZ:
                if (OrderDict.RoleType.TYPE_SGGR.equals(role) && user.getUserid().equals(chargehandId)){
                    //  安装中，安装完成 --> 上传照片
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("安装完成");
                    mBtnRight.setTag(OrderDict.ActionType.ACTION_AZWC);
                    mBtnRight.setEnabled(true);
                }
                break;
            case OrderDict.StatusType.DDZT_KHWFLX:
            case OrderDict.StatusType.DDZT_BKAZ:
                if (OrderDict.RoleType.TYPE_JCY.equals(role) && StringUtil.isEmpty(checkId)){
                    //  待抽查 -- > 抽查界面
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("监理抽查");
                    mBtnRight.setTag(OrderDict.ActionType.ACTION_JLCC);
                    mBtnRight.setEnabled(true);
                }else if (OrderDict.RoleType.TYPE_NQ.equals(role)||OrderDict.RoleType.TYPE_GRKH.equals(role)){
                    //  不通过，重新下单
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("重新下单");
                    mBtnRight.setTag(OrderDict.ActionType.ACTION_CXXD);
                    mBtnRight.setEnabled(true);
                }
                break;
            case OrderDict.StatusType.DDZT_AZWC:
                if (OrderDict.RoleType.TYPE_GRKH.equals(role) && StringUtil.isEmpty(commentId)){
                    //  待评论，立即评论 --> 打分，评论，上传照片
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("立即评论");
                    mBtnRight.setTag(OrderDict.ActionType.ACTION_LJPL);
                    mBtnRight.setEnabled(true);
                }else if (OrderDict.RoleType.TYPE_NQ.equals(role) && StringUtil.isEmpty(visitId)){
                    //  待回访，业主回访 --> 回访界面
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("业主回访");
                    mBtnRight.setTag(OrderDict.ActionType.ACTION_YZHF);
                    mBtnRight.setEnabled(true);
                }else if (OrderDict.RoleType.TYPE_JCY.equals(role) && StringUtil.isEmpty(checkId)){
                    //  待抽查 -- > 抽查界面
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("监理抽查");
                    mBtnRight.setTag(OrderDict.ActionType.ACTION_JLCC);
                    mBtnRight.setEnabled(true);
                }
                break;


            case OrderDict.StatusType.DDZT_DHF:
                if (OrderDict.RoleType.TYPE_NQ.equals(role) && StringUtil.isEmpty(visitId)){
                    //  待回访，业主回访 --> 回访界面
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("业主回访");
                    mBtnRight.setTag(OrderDict.ActionType.ACTION_YZHF);
                    mBtnRight.setEnabled(true);
                }
                break;
            case OrderDict.StatusType.DDZT_YHF:
            case OrderDict.StatusType.DDZT_YCC:
            case OrderDict.StatusType.DDZT_YPL:
                if (OrderDict.RoleType.TYPE_JCY.equals(role) && StringUtil.isEmpty(checkId)){
                    BottomLyt.setVisibility(View.VISIBLE);
                    mBtnRight.setVisibility(View.VISIBLE);
                    mBtnRight.setText("监理抽查");
                    mBtnRight.setTag(OrderDict.ActionType.ACTION_JLCC);
                    mBtnRight.setEnabled(true);
                }
                break;

        }

    }

    /**
     * 客户 只能看图片和评论
     * 内勤可以看 图片，评论，抽查和回访
     * 队长可以看 图片，评论和抽查
     * 工人可以看 图片，评论和抽查
     * 监理抽查只能查看抽查
     */
    @Override
    public void showMenu(ARResponse r) {
        String status = r.getValue("ORDER_STATUS");
        String role = UniApplication.getInstance().getCurrentRole();
        String visitId = r.getValue("VISIT_BY");
        String checkId = r.getValue("CHECK_BY");
//        String commentId = r.getValue("COMMENT_ID");
        String commentId = r.getValue("COMMENT_DATE");
        // TODO 状态待确认
        List<String> menus = new ArrayList<String>();
        switch (status){
            case OrderDict.StatusType.DDZT_DSZ:
            case OrderDict.StatusType.DDZT_BTG:
            case OrderDict.StatusType.DDZT_DCL:
            case OrderDict.StatusType.DDZT_KHWFLX:
            case OrderDict.StatusType.DDZT_DCF:
            case OrderDict.StatusType.DDZT_DAZ:
            case OrderDict.StatusType.DDZT_AZZ:
            case OrderDict.StatusType.DDZT_BKAZ:
                // 无菜单
                break;
            case OrderDict.StatusType.DDZT_AZWC:
                menus.add(OrderDict.MenuType.MENU_CKTP);
                if (OrderDict.RoleType.TYPE_NQ.equals(role)){
                    if(!StringUtil.isEmpty(visitId))
                        menus.add(OrderDict.MenuType.MENU_CKHF);
                    if(!StringUtil.isEmpty(checkId))
                        menus.add(OrderDict.MenuType.MENU_CKCC);
                }else if(OrderDict.RoleType.TYPE_JCY.equals(role)){
                    if(!StringUtil.isEmpty(checkId))
                        menus.add(OrderDict.MenuType.MENU_CKCC);
                }
                if(!StringUtil.isEmpty(commentId))
                    menus.add(OrderDict.MenuType.MENU_CKPL);
                break;
        }
        if(menus.size() > 0) {

            String[] strs = (String[]) menus.toArray(new String[menus.size()]);
            showSelectSheet(strs);
        }
    }

    @Override
    public void popWithbh(String orderjson) {
        final NormalViewDialog dialog = new NormalViewDialog(getActivity());
        final EditText editText = new EditText(getActivity());
        editText.setTextColor(Color.parseColor("#464646"));
        editText.setHint("请输入驳回原因");
        editText.setBackgroundResource(R.drawable.edit_bohui_bg);
        editText.setTextSize(13);
        editText.setSingleLine(true);
        editText.setPadding(10, 30, 10, 30);
        dialog.addContentView(editText);
        dialog.setCanceledOnTouchOutside(false);
        dialog.title("驳回原因 ")
                .titleTextSize(16)
                .style(NormalDialog.STYLE_TWO)
                .titleTextColor(Color.parseColor("#444444"))
                .titleTextSize(16)
                .showAnim(new ZoomInEnter())
                .dismissAnim(new ZoomOutExit())
                .cornerRadius(8)
                .show();



        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        if (editText.getText().toString().trim().equals("")) {
                            ActivityUtils.toast("驳回原因不能为空 ");
                        } else {
                            response.update("BH_REMARK",editText.getText().toString().trim());
                            mPresenter.doWithbohui(response.getData());
//                            RequestManager.getInstance().call(new OrderHandleApi(, respOrderBoHuiListener, errorListener));
//                            OKHttpUtils.formRequestWithParams(new OrderHandleApi.OrderHandleParams(getIntent().getStringExtra("order__id"),
//                                    orderDetailResult.getOrderStatus(),editText.getText().toString().trim(),""));
                        }
                        dialog.dismiss();
                    }
                });
    }

    @Override
    public void jumpWithpd(String orderjson) {
        final NormalDialog dialog = new NormalDialog(getActivity());
        dialog.setCanceledOnTouchOutside(false);
        dialog.content("确定审核通过此订单并派单吗?")
                .style(NormalDialog.STYLE_TWO)
                .title("")
                .titleTextSize(0)
                .contentTextSize(14)
                .contentTextColor(Color.parseColor("#444444"))
                .contentGravity(Gravity.CENTER)
                .showAnim(new ZoomInEnter())
                .dismissAnim(new ZoomOutExit())
                .cornerRadius(8)
                .show();

        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        Bundle bundle = new Bundle();
                        bundle.putString("response",response.toString());
                        ActivityUtils.startActivity(getActivity().getApplicationContext(),ShuiGPaiDanActivity.class,bundle);
                        dialog.dismiss();

                    }
                });

    }

    @Override
    public void jumpWithxd(String orderjson) {
        if(Constants.TYPE_SHUI_GUAN.equals(ordType)) {
            Bundle bundle = new Bundle();
            bundle.putString("response",response.toString());
            ActivityUtils.startActivity(getActivity(), ShuiGMakeOrderActivity.class,bundle);
        }else{
            Bundle bundle = new Bundle();
            bundle.putString("response",response.toString());
            ActivityUtils.startActivity(getActivity(), DiNuanMarkOrderActivity.class,bundle);
        }
    }

    @Override
    public void jumpWithjd(String orderjson) {
        if(Constants.TYPE_SHUI_GUAN.equals(ordType)) {
            Bundle bundle = new Bundle();
            bundle.putString("response",response.toString());
            ActivityUtils.startActivity(getActivity(),ShuiGPaiGongActivity.class,bundle);
        }else{
            Bundle bundle = new Bundle();
            bundle.putString("response",response.toString());
            ActivityUtils.startActivity(getActivity(),DiNuanPaiGongActivity.class,bundle);
        }
    }

    @Override
    public void jumpWithbkaz(String orderjson) {
        Bundle bundle = new Bundle();
        bundle.putString("response",response.toString());
        ActivityUtils.startActivity(getActivity(), ShuiGCannotInstallActivity.class,bundle);
    }

    @Override
    public void jumpWithwcaz(String orderjson) {
        if(Constants.TYPE_SHUI_GUAN.equals(ordType)) {
            Bundle bundle = new Bundle();
            bundle.putString("response",response.toString());
            ActivityUtils.startActivity(getActivity(), ShuiGCompleteActivity.class,bundle);
        }else {
            Bundle bundle = new Bundle();
            bundle.putString("response", response.toString());
            ActivityUtils.startActivity(getActivity(), DiNuanCompleteActivity.class, bundle);
        }
    }

    @Override
    public void jumpWithpl(String orderjson) {
        Bundle bundle = new Bundle();
        bundle.putString("response",response.toString());
        ActivityUtils.startActivity(getActivity().getApplicationContext(), ShuiGCommentActivity.class, bundle);
    }

    @Override
    public void jumpWithyzhf(String orderjson) {
        Bundle bundle = new Bundle();
        bundle.putString("response",response.toString());
        ActivityUtils.startActivity(getActivity().getApplicationContext(), ShuiGHuiFangActivity.class,bundle);
    }

    @Override
    public void jumpWithjlcc(String orderjson) {
        Bundle bundle = new Bundle();
        bundle.putString("response",response.toString());
        ActivityUtils.startActivity(getActivity(),ShuiGChouChaActivity.class,bundle);
    }

    private void showSelectSheet(final String[] stringItems) {
        ((BaseActivity)getActivity()).titleBar.removeAllActions();
        ((BaseActivity)getActivity()).titleBar.addAction(new TitleBar.ImageAction(R.mipmap.ic_more) {
            @Override
            public void performAction(View view) {

                final ActionSheetDialog dialog = new ActionSheetDialog(getActivity(), stringItems, null);
                dialog.isTitleShow(false).show();

                dialog.setOnOperItemClickL(new OnOperItemClickL() {
                    @Override
                    public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                        ActivityUtils.toast(stringItems[position]);
                        dialog.dismiss();
                        if (OrderDict.MenuType.MENU_CKTP.equals(stringItems[position])) {
                            // 查看订单图片
                            if(Constants.TYPE_SHUI_GUAN.equals(ordType)) {
                                Bundle bundle = new Bundle();
                                bundle.putString("response", response.toString());
                                ActivityUtils.startActivity(getActivity().getApplicationContext(), CompleteShowActivity.class, bundle);
                            }else{
                                Bundle bundle = new Bundle();
                                bundle.putString("response", response.toString());
                                ActivityUtils.startActivity(getActivity().getApplicationContext(), CompleteShowDiNuanActivity.class, bundle);
                            }
                        } else if (OrderDict.MenuType.MENU_CKPL.equals(stringItems[position])) {
                            // 查看评论
                            Bundle bundle = new Bundle();
                            bundle.putString("response",response.toString());
                            ActivityUtils.startActivity(getActivity().getApplicationContext(), CommentShowActivity.class, bundle);
                        } else if(OrderDict.MenuType.MENU_CKHF.equals(stringItems[position])){
                            // 查看回访
                            Bundle bundle = new Bundle();
                            bundle.putString("response",response.toString());
                            ActivityUtils.startActivity(getActivity().getApplicationContext(), HuiFangShowActivity.class, bundle);
                        } else if(OrderDict.MenuType.MENU_CKCC.equals(stringItems[position])){
                            // 查看抽查
                            Bundle bundle = new Bundle();
                            bundle.putString("response",response.toString());
                            ActivityUtils.startActivity(getActivity().getApplicationContext(), ChouChaShowActivity.class, bundle);
                        }
                    }
                });
            }
        });
    }
//
    @Override
    public void initView() {
        user = UniApplication.getInstance().getUserInfo();
        ordType = getArguments().getString("order_type");
    }

    View.OnClickListener clickListener = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            String action = v.getTag().toString();
            switch (action){
                case OrderDict.ActionType.ACTION_BH:
                    popWithbh(response.getData());
                    break;
                case OrderDict.ActionType.ACTION_TGPD:
                    jumpWithpd(response.getData());
                    break;
                case OrderDict.ActionType.ACTION_CXXD:
                    jumpWithxd(response.getData());
                    break;
                case OrderDict.ActionType.ACTION_WFLX:
                    mPresenter.doWithnoContact(response.getData());
                    break;
                case OrderDict.ActionType.ACTION_QRJD:
                    jumpWithjd(response.getData());
                    break;
                case OrderDict.ActionType.ACTION_LJCF:
                    mPresenter.doWithljcf(response.getData());
                    break;
                case OrderDict.ActionType.ACTION_BKAZ:
                    jumpWithbkaz(response.getData());
                    break;
                case OrderDict.ActionType.ACTION_KSAZ:
                    mPresenter.doWithksaz(response.getData());
                    break;
                case OrderDict.ActionType.ACTION_AZWC:
                    jumpWithwcaz(response.getData());
                    break;
                case OrderDict.ActionType.ACTION_LJPL:
                    jumpWithpl(response.getData());
                    break;
                case OrderDict.ActionType.ACTION_YZHF:
                    jumpWithyzhf(response.getData());
                    break;
                case OrderDict.ActionType.ACTION_JLCC:
                    jumpWithjlcc(response.getData());
                    break;
            }
        }
    };


    @Override
    public void onClickNinePhotoItem(BGASortableNinePhotoLayoutView ninePhotoLayout, View view, int position, String model, List<String> models) {
        startActivity(BGAPhotoPreviewActivity.newIntent(getActivity().getApplicationContext(), null, (ArrayList<String>) models,position));
    }

    @Override
    public boolean onLongClickNinePhotoItem(BGASortableNinePhotoLayoutView ninePhotoLayout, View view, int position, String model, List<String> models) {
        return false;
    }

    @Override
    public void showDiglog(String content){
        CustomProgress.show(getActivity(),StringUtil.isEmpty(content)?"正在处理...":content,true,null);
    }

    @Override
    public void hideDiglog(){
        CustomProgress.close();
    }

    @Override
    public void initListener() {
        mBtnLeft.setOnClickListener(clickListener);
        mBtnRight.setOnClickListener(clickListener);


        baseAdapter.setViewBinder(new ViewBinder() {
            @Override
            public boolean setViewValue(View view, Object data, int position, String from) {
                ImageView phone = (ImageView) view.findViewById(R.id.plan_item_phone);
                TextView tv = (TextView) view.findViewById(R.id.plan_item_detail);
                if (from.equals("CREATE_TYPE")) {
                    tv.setText(data.toString().equals("1")?"客户":"内勤");
                }
                return false;
            }
        });

        clientAdapter.setViewBinder(new ViewBinder() {
            @Override
            public boolean setViewValue(View view, Object data, int position, String from) {
                ImageView phone = (ImageView) view.findViewById(R.id.plan_item_phone);
                TextView tv = (TextView) view.findViewById(R.id.plan_item_detail);
                if(from.equals("CONTACT_PHONE")){
                    if (response != null && !response.getValue("CONTACT_PHONE").equals(user.getPhone()))
                        phone.setVisibility(View.VISIBLE);
                }else if(from.equals("INSTALL_DATE")){
                    if(response != null){
                        String status = response.getValue("ORDER_STATUS");
                        String tempdate = data.toString();
                        switch (status){
                            case OrderDict.StatusType.DDZT_DSZ:
                            case OrderDict.StatusType.DDZT_BTG:
                            case OrderDict.StatusType.DDZT_DCL:
                            case OrderDict.StatusType.DDZT_KHWFLX:
                            case OrderDict.StatusType.DDZT_BKAZ:
                                tempdate = response.getValue("INSTALL_DATE");
                                break;
                            default:
                                tempdate = response.getValue("APPOINTMENT_DATE") + " " +response.getValue("APPOINTMENT_TIME");
                                break;
                        }
                        tv.setText(StringUtil.isNotEmpty(tempdate)?tempdate:data.toString());
                    }
                    tv.setTextColor(getResources().getColor(R.color.text_contactphone));
                }else if(from.equals("PAY_TYPE")  || from.equals("INSTALL_CONTENT")){
                    Dic dic = SQLDao.getInstance(getActivity().getApplicationContext()).selectWithType(data+"");
                    if(dic != null)
                        tv.setText(dic.getName());
                    else
                        tv.setText(data+"");
                }else if(from.equals("PRODUCT_TYPE")){
                    Dic dic = SQLDao.getInstance(getActivity().getApplicationContext()).selectProWithType(data+"");
                    if(dic != null)
                        tv.setText(dic.getName());
                    else
                        tv.setText(data+"");
                }else if(from.equals("ADDR_DETAIL")){
                    if(response != null) {
                        tv.setText(response.getValue("ADDR_AREA") + " " + response.getValue("ADDR_DETAIL"));
                    }
                }
                return false;
            }
        });
        statusAdapter.setViewBinder(new ViewBinder() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public boolean setViewValue(View view, Object data, int position, String from) {
                ImageView phone = (ImageView) view.findViewById(R.id.plan_item_phone);
                TextView tv = (TextView) view.findViewById(R.id.plan_item_detail);
                if(from.equals("INNER_NAME")){
                    if(response != null && !response.getValue("INNER_PHONE").equals(user.getPhone()))
                        phone.setVisibility(View.VISIBLE);
                }else if(from.equals("CAPTAIN_NAME")){
                    if(response != null && !response.getValue("CAPTAIN_PHONE").equals(user.getPhone()))
                        phone.setVisibility(View.VISIBLE);
                }else if(from.equals("CHARGEHAND_NAME")){
                    if(response != null && !response.getValue("CHARGEHAND_PHONE").equals(user.getPhone()))
                        phone.setVisibility(View.VISIBLE);
                }else if(from.equals("CHECK_NAME")){
                    if(response != null && !response.getValue("CHECK_PHONE").equals(user.getPhone()))
                        phone.setVisibility(View.VISIBLE);
                }else if(from.equals("ORDER_STATUS")){
                    tv.setTextColor(getResources().getColor(R.color.text_contactphone));
                    tv.setText(OrderDict.getNameByStatus(data.toString()));
                }else if(from.equals("CAPTAIN_REMARK")){
                    tv.setTextColor(getResources().getColor(R.color.text_contactphone));
                }else if(from.equals("REFUSE_REASON")  || from.equals("ZGDGG")){
                    Dic dic = SQLDao.getInstance(getActivity().getApplicationContext()).selectWithType(data+"");
                    if(dic != null)
                        tv.setText(dic.getName());
                    else
                        tv.setText(data+"");
                }else if(from.equals("PIC_REFUSE")&& StringUtil.isNotEmpty((String)data)){
                        view.setLayoutParams(new AbsListView.LayoutParams(
                                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
                        view.findViewById(mTo[1]).setVisibility(View.GONE);
//                    ((RelativeLayout.LayoutParams)view.findViewById(R.id.view_line).getLayoutParams()).addRule(RelativeLayout.BELOW, R.id.gridview);
                        LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    /*layoutparams.addRule(RelativeLayout.BELOW, R.id.lyt_top);
                    layoutparams.addRule(RelativeLayout.ABOVE, R.id.view_line);
                    layoutparams.setMargins(10,10,10,10);*/
                        layoutparams.gravity = Gravity.CENTER;
                        layoutparams.setMargins(10, 10, 10, 10);

                        BGASortableNinePhotoLayoutView ninePhotoLayout = new BGASortableNinePhotoLayoutView(getActivity().getApplicationContext());
                        ninePhotoLayout.init(getActivity().getApplicationContext());
                        ninePhotoLayout.setDelegate(fragment);
                        ninePhotoLayout.setLayoutParams(layoutparams);
                        ninePhotoLayout.setId(R.id.gridview);
                        ninePhotoLayout.setData(StringUtil.getListWithPath(data.toString(), ","));
                        ninePhotoLayout.setDelegate(ShuiGFragment.this);
//                    Log.e("ccqx","ddd:"+StringUtil.getListWithPath(data.toString(),","));
//                    ((ViewGroup)view.findViewById(R.id.lyt_top)).addView(ninePhotoLayout);
                        ((ViewGroup) view).addView(ninePhotoLayout, 1);
                    }
                return false;
            }
        });

        clientAdapter.setCJRowListener(new CJRowListener() {
            @Override
            public boolean seedAction(int location, Field field, Object obj) {
                return false;
            }

            @Override
            public boolean activityAction(Field field, Object obj) {
                String from = field.getFieldName();
                if(from.equals("CONTACT_PHONE")){
                    call(obj.toString());
                }
                return false;
            }

            @Override
            public boolean popupWindowAction(IField field, Object obj) {
                return false;
            }
        });

        statusAdapter.setCJRowListener(new CJRowListener() {
            @Override
            public boolean seedAction(int location, Field field, Object obj) {
                return false;
            }

            @Override
            public boolean activityAction(Field field, Object obj) {
                String from = field.getFieldName();
                if(from.equals("INNER_NAME")){
                    call(response.getValue("INNER_PHONE"));
                }else if(from.equals("CAPTAIN_NAME")){
                    call(response.getValue("CAPTAIN_PHONE"));
                }else if(from.equals("CHARGEHAND_NAME")){
                    call(response.getValue("CHARGEHAND_PHONE"));
                }else if(from.equals("CHECK_NAME")){
                    call(response.getValue("CHECK_PHONE"));
                }
                return false;
            }

            @Override
            public boolean popupWindowAction(IField field, Object obj) {
                return false;
            }
        });
    }

    @Override
    public void initData() {
        table_base = ShuiGBuz.getBaseTable();
        String role = UniApplication.getInstance().getCurrentRole();
        if(Constants.TYPE_SHUI_GUAN.equals(ordType)){
            table_client = ShuiGBuz.getClientTable();
            table_status = ShuiGBuz.getStatusTable();
        }else{
            table_client = ShuiGBuz.getClientDNTable();
            table_status = ShuiGBuz.getStatusDNTable();
            if(OrderDict.RoleType.TYPE_GRKH.equals(role))
                ShuiGBuz.getUserStatusTable(table_status);
        }

        baseAdapter = new CustomCJRowAdapter(getActivity(), this.table_base, ShuiGBuz.getMockBaseResponse(),
                layout, mTo);
        clientAdapter = new CustomCJRowAdapter(getActivity(), this.table_client, ShuiGBuz.getMockBaseResponse(),
                layout, mTo);
        statusAdapter = new CustomCJRowAdapter(getActivity(), this.table_status, ShuiGBuz.getMockBaseResponse(),
                layout, mTo);
        mRowBase.setAdapter(baseAdapter);
        mRowClient.setAdapter(clientAdapter);
        mRowStatus.setAdapter(statusAdapter);
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_shuiguan_details;
    }

}
